package com.team.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.entity.AppInfo;
import com.team.mapper.AppInfoMapper;
import com.team.mapper.AppVersionMapper;
import com.team.service.AppInfoService;
import com.team.utils.AppInfoCondition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AppInfoServiceImpl implements AppInfoService {
    @Autowired
    private AppInfoMapper appInfoMapper;
    @Autowired
    private AppVersionMapper appVersionMapper;
    @Override
    public PageInfo<AppInfo> getAllBasicAPPInformation(AppInfoCondition appInfoCondition) {
        PageHelper.startPage(appInfoCondition.getPage(), appInfoCondition.getPageSize());

        List<AppInfo> allBasicAPPInformation = appInfoMapper.findAllBasicAPPInformation(appInfoCondition);

        PageInfo<AppInfo> appInfoPageInfo = new PageInfo<>(allBasicAPPInformation);

        return appInfoPageInfo;

    }

    @Override
    public int addAppInfo(AppInfo appInfo) {
        return appInfoMapper.insertSelective(appInfo);
    }

    @Override
    public AppInfo getSingleAppInfo(Long id) {
        return appInfoMapper.selectByPrimaryKey2(id);
    }

    @Override
    public int updateAppInfo(AppInfo appInfo) {
        return appInfoMapper.updateByPrimaryKeySelective(appInfo);
    }

    @Override
    @Transactional  //基于事务执行业务方法中不能添加try。。。catch捕获异常
    public void delAppInfo(Long id) {
        //先删外键，再删主键
        //删除app版本 编写dao层
        //删除app信息
        appVersionMapper.deleteAppVersionByAppId(id);

        appInfoMapper.deleteByPrimaryKey(id);


    }

    @Override
    public PageInfo<AppInfo> getAppInfoByStatus(AppInfoCondition appInfoCondition) {
        PageHelper.startPage(appInfoCondition.getPage(),appInfoCondition.getPageSize());

        List<AppInfo> allAppInfoByState = appInfoMapper.getAllAppInfoByState(appInfoCondition);

        PageInfo<AppInfo> appInfoPageInfo = new PageInfo<>(allAppInfoByState);

        return appInfoPageInfo;
    }

    @Override
    public AppInfo getAppInfoForChecked(Long id) {
        return appInfoMapper.getAllAppInfoByCondition(id);
    }

    @Override
    public int updateAppStatus(Long id, Long status) {
        AppInfo appInfo = new AppInfo();
        appInfo.setId(id);
        appInfo.setStatus(status);
        return appInfoMapper.updateByPrimaryKeySelective(appInfo);
    }
}
